package com.gitee.mrj.aiboot.core.sf.generators.ui.widget;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.sf.ui.widget.SFMentionWidget;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class SFMentionWidgetGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        SFMentionWidget widget = AnnotatedElementUtils.findMergedAnnotation(field, SFMentionWidget.class);
        generate(node, widget);
    }

    private void generate(UIForm node, SFMentionWidget widget) {
        ObjectNode ui = node.getUi();

        ui.put("widget", "mention");
        putString(ui, "placeholder", widget.placeholder());
        putString(ui, "notFoundContent", widget.notFoundContent());
        ui.put("placement", widget.placement().type());
        if (ArrayUtils.isNotEmpty(widget.prefix())) {
            ArrayNode array = putArray(ui, "prefix");
            for (String prefix : widget.prefix()) {
                array.add(prefix);
            }
        }
        ui.put("inputStyle", widget.inputStyle().type());

        putInt(ui, "minRows", widget.minRows());
        putInt(ui, "maxRows", widget.maxRows());
        ui.put("autosize", widget.autosize());
    }

    @Override
    public Class[] supports() {
        return new Class[]{SFMentionWidget.class};
    }
}
